package services;

import dao.ArticleInfoDao;
import models.UserInfo;
import utils.ResultUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;

/**
 * Created with IDEA
 * author:ju bai
 * Date:2021/4/11
 * Time:9:19
 **/
@WebServlet("/addart")
public class AddArtServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int succ = -1;
        String msg = "" ;
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        if( title != null && content != null && !title.equals("") && !content.equals("")) {
            HttpSession session = req.getSession(false);
            if(session != null && session.getAttribute("userinfo") != null){
                UserInfo userInfo = (UserInfo) session.getAttribute("userinfo");//获取与指定名字相联系的属性值
                ArticleInfoDao articleInfoDao = new ArticleInfoDao();
                try {
                    succ = articleInfoDao.addart(title,content,userInfo.getId());
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }else{
                msg="参数不全!";
            }
            HashMap<String,Object> res = new HashMap<>();
            res.put("succ",succ);
            res.put("msg",msg);
            ResultUtils.write(resp,res);
        }
    }
}
